//判断是否登录
var user=localStorage.getItem("user");
console.log($(".wel>span"));
if(user){
    $(".wel>p").eq(1).css("display","none");
    $(".wel>p").eq(0).css("display","block");
    $(".wel span").html(JSON.parse(user)[0].username);
    $("a").click(function(){
        localStorage.removeItem("user");
    })
//插入商品
var addbtn=$(".addgoods>button");
addbtn.click(function(){
    if($("input").val()){
        var pimg=$("input:eq(0)").val();
        var pname=$("input:eq(1)").val();
        var pprice=$("input:eq(2)").val();
        var pdesc=$("input:eq(3)").val();
        var uid=JSON.parse(localStorage.getItem("user"))[0].uid;
        axios.get(AddgoodsAPI,{params:{pimg,pname,pprice,pdesc,uid}}).then(function(res){
            alert(res.data.msg);
            showgoods();
            $("input").val("");
        })
    }else{
        alert("请检查输入信息");
    }
})
//展示商品
//http://jx.xuzhixiang.top/ap/api/allproductlist.php?pagesize=20&pagenum=4
function showgoods(){
    var pagesize=100;
    var pagenum=0;
    var uid=JSON.parse(localStorage.getItem("user"))[0].uid;
    var html="";
    axios.get(ShowAPI,{params:{pagesize,pagenum,uid}}).then(function(res){
        res.data.data.forEach(v => {
            html+= `<ul>
            <li><img src="${v.pimg}" alt="" /></li>
            <li>${v.pname}</li>
            <li>${v.pprice}</li>
            <li>${v.pdesc}</li>
            <li><button onclick="delbtn(${v.pid})">删除</button>
            <button onclick="change(${v.pid})">修改信息</button>
            </li>
          </ul>`
        });
        $(".addshow").html(html);
    })
}
showgoods();

//删除商品
function delbtn(pid){
    var uid=JSON.parse(localStorage.getItem("user"))[0].uid;
    var token=JSON.parse(localStorage.getItem("user"))[0].token;
    axios.get(DelAPI,{params:{pid,uid,token}}).then(function(res){
        showgoods();
    })
}

//修改信息 跳转新页面
function change(pid){
    location.href=`changemsg.html?pid=${pid}`;
}
}else{
    $(".wel p").eq(0).css("display","none");
    $(".wel p").eq(1).css("display","block");
    $(".addgoods>button").click(function(){
        alert("请先登录");
    })
}